package com.five.calendar.ui.activity;

import android.app.ActivityOptions;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.five.calendar.R;
import com.five.calendar.base.BaseActivity;
import com.five.calendar.base.BaseFragment;
import com.five.calendar.function.goals.GoalsFragment;
import com.five.calendar.function.home.HomeFragment;
import com.five.calendar.function.more.MoreFragment;
import com.five.calendar.function.schedule.ScheduleFragment;
import com.five.calendar.ui.adapter.ViewPagerAdapter;
import com.five.calendar.ui.adapter.YearAdapter;
import com.five.calendar.ui.calendarview.model.CalendarDate;
import com.five.calendar.ui.tab.NormalTab;
import com.five.calendar.ui.tab.RoundTab;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import me.majiajie.pagerbottomtabstrip.NavigationController;
import me.majiajie.pagerbottomtabstrip.PageNavigationView;
import me.majiajie.pagerbottomtabstrip.listener.OnTabItemSelectedListener;

/**
 * 主activity
 * Created by zhuxiaoxin on 2017/8/26.
 */
public class MainActivity extends BaseActivity implements OnTabItemSelectedListener {
    @BindView(R.id.calendar_main_viewpager)
    ViewPager mViewPager;
    @BindView(R.id.calendar_main_tab)
    PageNavigationView mBottomTab;
    @BindView(R.id.start_mark)
    RelativeLayout mStartMark;
    @BindView(R.id.start_schedule)
    ImageView mStartSchedule;
    @BindView(R.id.start_goal)
    ImageView mStartGoal;
    @BindView(R.id.calendar_toolbar_left)
    ImageView mYearView;
    @BindView(R.id.action_bar_layout)
    RelativeLayout mActionBarLayout;
    @BindView(R.id.calendar_toolbar_title)
    TextView mCalendarTitle;
    private ViewPagerAdapter mPagerAdapter;
    private ArrayList<BaseFragment> mFragments;
    private int mCurrent = 0;
    private NormalTab /**mRoundTab,*/
            mHomeTab, mScheduleTab, mGoalTab, mMoreTab;
    private RoundTab mRoundTab;
    private boolean mHasDown;
    private HomeFragment mHomeFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        }

        initializeFragments();

        mPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager(), mFragments);
        mViewPager.setAdapter(mPagerAdapter);
        mViewPager.setOffscreenPageLimit(3);
        initializeBottomTab();

    }

    @Override
    protected void onResume() {
        super.onResume();
        mStartMark.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                mStartMark.setVisibility(View.GONE);
                return true;
            }
        });
        mStartSchedule.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ScheduleAddActivity.startScheduleAddActivity();
                mStartMark.setVisibility(View.GONE);
            }
        });
        mStartGoal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                GoalAddActivity.startGoalAddActivity();
                mStartMark.setVisibility(View.GONE);
            }
        });
        CalendarDate calendarDate = new CalendarDate();
        mCalendarTitle.setText(calendarDate.getYear() + "-" + calendarDate.getMonth());
    }

    @OnClick(R.id.calendar_toolbar_left)
    void toolBarLeftClick() {
//        startActivityForResult(new Intent(this, YearActivity.class), 1);
//        Toast.makeText(this, "11", Toast.LENGTH_SHORT).show();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            startActivityForResult(new Intent(this, YearActivity.class), 1, ActivityOptions.makeSceneTransitionAnimation(this).toBundle());
        } else {
            startActivityForResult(new Intent(this, YearActivity.class), 1);
        }
    }

    private void initializeBottomTab() {
        mRoundTab = newRoundItem(R.drawable.add_layer_pic, R.drawable.add_layer_pic, "");
        mRoundTab.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    mHasDown = true;
                }
                if (event.getAction() == MotionEvent.ACTION_UP && mHasDown) {
                    if (mStartMark.getVisibility() == View.VISIBLE) {
                        mStartMark.setVisibility(View.GONE);
                    } else {
                        mStartMark.setVisibility(View.VISIBLE);
                    }
                    mHasDown = false;
                }
                return true;
            }
        });
        mHomeTab = newItem(R.drawable.ic_home, R.drawable.ic_home_s, "Home");
        mScheduleTab = newItem(R.drawable.ic_schedule, R.drawable.ic_schedule_s, "Schedule");
        mGoalTab = newItem(R.drawable.ic_goals, R.drawable.ic_goals_s, "Goal");
        mMoreTab = newItem(R.drawable.ic_more, R.drawable.ic_more_s, "More");
        NavigationController navigationController = mBottomTab.custom()
                .addItem(mHomeTab).addItem(mScheduleTab).addItem(mRoundTab).addItem(mGoalTab)
                .addItem(mMoreTab).build();
        navigationController.addTabItemSelectedListener(this);
    }

    private void initializeFragments() {
        if (mFragments == null) {
            mFragments = new ArrayList<>();
        }
        mHomeFragment = new HomeFragment();
        mFragments.add(mHomeFragment);
        mFragments.add(new ScheduleFragment());
        mFragments.add(new GoalsFragment());
        mFragments.add(new MoreFragment());
    }

    /**
     * 正常tab
     */
    private NormalTab newItem(int drawable, int checkedDrawable, String text) {
        NormalTab mainTab = new NormalTab(this);
        mainTab.initialize(drawable, checkedDrawable, text);
        mainTab.setTextDefaultColor(0xFF888888);
        mainTab.setTextCheckedColor(0xFF009688);
        return mainTab;
    }

    /**
     * 圆形tab
     */
    private RoundTab newRoundItem(int drawable, int checkedDrawable, String text) {
        RoundTab mainTab = new RoundTab(this);
        mainTab.initialize(drawable, checkedDrawable, text);
        mainTab.setTextDefaultColor(0xFF888888);
        mainTab.setTextCheckedColor(0xFF009688);
        return mainTab;
    }


    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        Fragment fragment = mPagerAdapter.getItem(mViewPager.getCurrentItem());
        if (fragment instanceof IOnWindowFocusChangeListener) {
            ((IOnWindowFocusChangeListener) fragment).onWindowFocusChanged(hasFocus);
        }
    }

    @Override
    public void onSelected(int i, int i1) {
        if (i == 0) {
            mActionBarLayout.setVisibility(View.VISIBLE);
            mCurrent = 0;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                getWindow().setStatusBarColor(0xffffffff);
            }
        } else if (i == 1) {
            mActionBarLayout.setVisibility(View.VISIBLE);
            mCurrent = 1;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                getWindow().setStatusBarColor(0xffffffff);
            }
        } else if (i == 2) {
            //nothing
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                getWindow().setStatusBarColor(0xffffffff);
            }
        } else if (i == 3) {
            mActionBarLayout.setVisibility(View.GONE);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                getWindow().setStatusBarColor(0xffffffff);
            }
            mCurrent = 2;
        } else if (i == 4) {
            mActionBarLayout.setVisibility(View.GONE);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                getWindow().setStatusBarColor(0xff4fb2ff);
            }
            mCurrent = 3;
        }
        mViewPager.setCurrentItem(mCurrent);
    }

    @Override
    public void onRepeat(int i) {

    }

    public void setCalendarTitle(String title){
        Log.e("ZXX","setTitle: " + title);
        mCalendarTitle.setText(title);
    }


    public interface IOnWindowFocusChangeListener {
        void onWindowFocusChanged(boolean hasFocus);
    }

    @Override
    public void onBackPressed() {
        if (mStartMark.getVisibility() == View.VISIBLE) {
            mStartMark.setVisibility(View.GONE);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1) {
            if (data != null){
                int year = data.getIntExtra("year", new CalendarDate().getYear());
                int month = data.getIntExtra("month", new CalendarDate().getMonth());
                mHomeFragment.setCalendarDate(year, month);
            }
        } else if (requestCode == 2) {
            mHomeFragment.initCalendarView();
        }

    }
}
